package com.surfilter.msmp.common.filter;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName: HTMLPageCharacterFilter
 *
 * @Description: HTML页面特征过滤器，通过获取网站某一类页面的特征词，来获取页面
 *
 * @author JinHuaTao
 * 
 * @date 2016年6月2日 上午9:24:29
 * 
 */
public class HpcFilter {

	private static final Logger logger = LoggerFactory.getLogger(HpcFilter.class);

	/**
	 * @Title: matchRule @Description: 正则解析 @param src 过滤的内容 @param rule
	 *         正则表达式 @return boolean @throws
	 */
	private static boolean matchRule(String src, String rule) {
		boolean result = false;
		Pattern pattern = Pattern.compile(rule.trim());
		Matcher m = pattern.matcher(src);
		if (m.find()) {
			result = true;
		}
		return result;
	}

	/**
	 * 词组每个词都必须匹配上
	 * */
	public static boolean everyWordMustMatch(String html, String[] filterWords) {
		boolean result = false;
   		for (String word : filterWords) {
  			if (matchRule(html, word)) {// 必须全部匹配
 				result = true;
 			} else {
				result = false;
				break;
			}
		}
		return result;
	}

	/**
	 * 词组需要一个词必须匹配上
	 * */
	public static boolean oneWordShouldMatch(String html, String[] filterWords) {
		boolean result = false;
		for (String word : filterWords) {
			if (matchRule(html, word)) {// 匹配一组即可
				result = true;
				break;
			}
		}
		return result;
	}

	/**
	 * 词组没有词匹配上
	 * */
	public static boolean notOneWordMatch(String html, String[] filterWords) {
		return !oneWordShouldMatch(html, filterWords);
	}

	/**
	 * @Title: pageCharacterFilter @Description:
	 *         页面特征过滤器，通过收集网站某一类页面的特定关键词，来获取该类页面解析入库 @param html @param
	 *         filterWords @return boolean @throws
	 */
	private static boolean pageCharacterFilter(final String html, List<String[]> filterWords) {
		boolean result = false;

		if (null != filterWords && !filterWords.isEmpty()) {
			for (String[] filterWord : filterWords) {
				if (everyWordMustMatch(html, filterWord)) {
					result = true;
				}
			}
		}

		return result;
	}
	  /**
     * 几组词中，只要一组词被全部包含，则返回true
     * */
    private static boolean shouldFilter(final String html,List<String[]> filterWords){
        int counter = 0;
        if(null != filterWords && !filterWords.isEmpty()){
            for(String[] filterWord: filterWords){
                if(everyWordMustMatch(html, filterWord)){
                    counter++;
                }
            }
        }
        if(counter > 0){
			return true;
		}
        return false;
    }

	/**
	 * 根据HTML内容返回对应的类型
	 * */
	public static SSR_TYPE getSsrTypeByHtml(final String html){
		for(SSR_TYPE ssrType: SSR_TYPE.values()){
			if(everyWordMustMatch(html, ssrType.getVector())){
				return ssrType;
			}
		}
		return SSR_TYPE.NONE;
	}

	/**
	 * 根据分数返回对应的类型
	 *
	 * */
	public static SSR_TYPE getSsrTypeByScore(final String scoreStr){
		try{
			int score = Integer.parseInt(scoreStr);
			for(SSR_TYPE ssrType: SSR_TYPE.values()){
				if(score == ssrType.boost){
					return ssrType;
				}
			}
		}catch (Exception e){
			logger.info("分数处理异常{}.", e);
		}
		return HpcFilter.SSR_TYPE.NONE;
	}

	public enum SSR_TYPE{
		NONE("", new String[]{}, 0),
		A1("A1", new String[]{"菜单","首页","TOS","邀请码","登录","注册","STAFF"}, 60001),
		A2("A2", new String[]{"html5up.net","ajlkn","copyright"}, 60002),
		A3("A3", new String[]{"hostino","container","top-content","main-slider"}, 60003),
		A4("A4", new String[]{"gt.js","project.min.js","/auth/login","/auth/register"}, 60004),
		B1("B1", new String[]{"material","/auth/login","/auth/register","fonts.googleapis.com"}, 70001),
		B2("B2", new String[]{"material","/auth/login","/auth/register","staff"}, 70002),
		B3("B3", new String[]{"material","fonts.googleapis.com","project.min.js","base.min.js"}, 70003),
		C1("C1", new String[]{"sspanel","邀请码","navMenuColorprimary","hero","starloop"}, 70006);

		private String[] vector;
		private int boost;
		private String type;

		SSR_TYPE(String type, String[] vector, int boost) {
			this.type = type;
			this.vector = vector;
			this.boost = boost;
		}

		public String[] getVector() {
			return vector;
		}

		public int getBoost() {
			return boost;
		}

		public String getType() {
			return type;
		}
	}

}
